//
//  JiaoYiOrderBuyViewController.m
//  ChinaA
//
//  Created by Mxionlly on 2018/11/6.
//  Copyright © 2018 Evan. All rights reserved.
//

#import "JiaoYiOrderBuyViewController.h"
#import "taskOverviewService.h"
#import "OrderQiuGouTableViewCell.h"
#import "JiaoYiOrderDetailViewController.h"

@interface JiaoYiOrderBuyViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UITableView    *tableView;
@property (nonatomic,assign)NSInteger      page;
@property (nonatomic,strong)NSMutableArray *orderList;
@end

@implementation JiaoYiOrderBuyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.view addSubview:self.tableView];
    [self loadOrder:1];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    self.hidesBottomBarWhenPushed = NO;
    self.tabBarController.tabBar.hidden = YES;
    self.navigationController.navigationBar.hidden = NO;
}

-(void)loadData{
    self.page = 1;
    [self loadOrder:1];
}

-(void)loadMoreData{
    self.page ++;
    [self loadOrder:self.page];
}

-(void)loadOrder:(NSInteger) pageIndex{
    NSMutableDictionary *dict = [NSMutableDictionary new];
    [dict setField:[NSString stringWithFormat:@"%ld",self.page] value:@"pageindex"];
    [dict setField:@"10" value:@"pagesize"];
    [dict setField:@"1" value:@"ordertype"];//0：获取卖出订单，1：获取买入订单
    [dict setField:@"0" value:@"orderStatus"];
    [dict setField:@"0" value:@"isAll"];
    [taskOverviewService getOrderBuyAndSaleList:dict success:^(dataOrderBuyAndSale *response, BOOL success) {
        [DKProgressHUD dismiss];
        if (success) {
            self.orderList = response.data;
            [self.tableView reloadData];
            return ;
        }else{
            [DKProgressHUD showErrorWithStatus:response.message];
        }
    } failture:^(NSError *error, NSString *strDescription) {
        [DKProgressHUD dismiss];
        [DKProgressHUD showErrorWithStatus:strDescription];
    }];
}


#pragma mark - tableview

-(UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight-64) style:UITableViewStyleGrouped];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        _tableView.backgroundColor  = [UIColor clearColor];
        _tableView.scrollEnabled = YES;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
    }
    return _tableView;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.orderList count];
}

-(CGFloat )tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.5;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    OrderQiuGouTableViewCell *cell = [OrderQiuGouTableViewCell cellWithTableView:tableView IndexPath:indexPath];
    dataOrderBuyAndSaleInfo *model = [dataOrderBuyAndSaleInfo mj_objectWithKeyValues:[self.orderList objectAtIndex:indexPath.row]];
    cell.labelName.text = model.UserName;
    cell.labelPrice.text = [NSString stringWithFormat:@"单价: %@ CNY ",model.PriceRMB];
    cell.labelTime.text = model.CreateTimeName;
    cell.labelCNY.text = [NSString stringWithFormat:@"%@ CNY ",model.PriceCountRMB];
    cell.lblimit.text = @"";//[NSString stringWithFormat:@"限额: %@-%@ CNY ",model.LimitMix,model.LimitMax];
    cell.lbnum.text = [NSString stringWithFormat:@"数量: %@ china-A ",model.ChinaANumber];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    dataOrderBuyAndSaleInfo *model = [dataOrderBuyAndSaleInfo mj_objectWithKeyValues:[self.orderList objectAtIndex:indexPath.row]];
    [self.navigationController pushViewController:[[JiaoYiOrderDetailViewController alloc]initWithType:1 model:model] animated:YES];
}
@end
